<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no" />
  <link rel="stylesheet" type="text/css" href="css/report.css" />
  <link rel="stylesheet" type="text/css" href="css/swiper.min.css" />
  <title>Report</title>
</head>

<body>
  <div class="page">
    <div class="title-row-top" id="title-row"></div>
    <div class="time-toprow" id="time-toptext">
    </div>
    <div class="tab-row">
      <div class="tabs">
        <div class="day active" id="day">
          <div class="tab-text">1 day</div>
        </div>
        <div class="line_1" id="line_1"></div>
        <div class="week" id="week">
          <div class="tab-text">1 week</div>
        </div>
        <div class="line_2" id="line_2"></div>
        <div class="month" id="month">
          <div class="tab-text">1 month</div>
        </div>
        <div class="line_3" id="line_3"></div>
        <div class="year" id="year">
          <div class="tab-text">1 year</div>
        </div>
      </div>
    </div>
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide swiper-no-swiping">
          <!--日报-->
          <div class="card_1" id="card1_1" style="display: block">
            <div class="title-row" style="height: 28%">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-week2" id="title-text-day">0 hr 0 min</div>
            </div>
            <div class="subtext-week" id="day_subtext"></div>
            <div class="card-line"></div>
            <div class="deep stage-row">
              <div class="deep-dot"></div>
              <div class="deep-text">Deep</div>
              <div class="deep-data" id="deep-data">0 hr 0 min</div>
            </div>
            <div class="light stage-row">
              <div class="light-dot" style="background: #50AFCF"></div>
              <div class="light-text">Light</div>
              <div class="light-data" id="light-data">0 hr 0 min</div>
            </div>
            <div class="awake stage-row">
              <div class="awake-dot" style="background:#57A3A3"></div>
              <div class="awake-text">Awake</div>
              <div class="awake-data" id="awake-data">0 hr 0 min</div>
            </div>
          </div>
          <div class="card2_1">
            <div class="title-row2">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-day" id="title-text-day">0 hr 0 min</div>
            </div>
            <div class="card-line2"></div>
            <div class="deep-alone stage-row">
              <div class="deep-data2" id="deep-data0" style="width: 80%; text-align: left">
                0% of your evening rest
              </div>
            </div>
          </div>
          <div class="card3_1" style="height: 23.7vw">
            <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
            <div class="title-text-week1" id="title-text-day">0 hr 0 min</div>
            <div class="subtext-week" style="top: 60%">24 hour average</div>
          </div>
          <div class="card4_1" style="display: none; min-height: 43.2vw">
            <div class="title-row4" style="margin-bottom: 4%">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-day" id="title-text-day">0 hr 0 min</div>
            </div>
            <div class="card-line4"></div>
            <div class="text-tow" id="snore-row">
              <!-- <div class="row">
                  <div class="snore_time"></div>
                  <div class="time_length"></div>
                </div> -->
            </div>
          </div>
          <div class="card5_1" style="display: none; height: 43.2vw">
            <div class="title-row">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-day" id="title-text-day">0 hr 0 min</div>
            </div>
            <div class="card-line"></div>
            <div class="deep stage-row">
              <div class="deep-dot" style="background-color: #f5af00"></div>
              <div class="deep-text">Large movements</div>
              <div class="deep-data" id="deep-data">0 times</div>
            </div>
            <div class="light stage-row" style="margin-top: 4%">
              <div class="light-dot" style="background: #cacece"></div>
              <div class="light-text">Small movements</div>
              <div class="light-data" id="light-data">0 times</div>
            </div>
          </div>
          <div class="card_2" id="card_2">
            <div class="time-row">
              <div class="left-time" id="left-time"></div>
              <div class="right-time" id="right-time"></div>
            </div>
            <div class="card_chart" id="chart_awake" style="margin-top: 10%; height: 25%"></div>
            <div class="card_chart" id="chart_light" style="height: 25%"></div>
            <div class="card_chart" id="chart_deep"></div>
          </div>
          <div class="snoretips_card" id="snoretips_card" style="display: block">
            <div class="top-row">
              <img class="snore_img" src="image/snore_img.png" />
              <div class="top-text" id="top-text1">0 Auto Adjustments</div>
            </div>
            <div class="card-line5"></div>
            <div class="tips-content">
              Your bed automatically adjusted to the Dawn House pre-programmed Anti Snore position. This helps open your
              lungs and airway, resulting in calmer, deeper breathing.
            </div>
          </div>
          <div class="card_3">
            <div class="detail-title">
              Reaching all of the stages of the sleep cycle is crucial to your
              health and well-being.
            </div>
            <div class="detail-content">
              The cycles include, Light Sleep, which happens no matter how
              much sleep you get, Deep Sleep, which in many ways is more
              restorative. Together the stages make up the sleep cycle which
              works to heal and restore the body.
            </div>
          </div>
          <div class="hrvcard size1">
            <div class="hrv_title">LF/HF Nervous System Function</div>
            <div class="hrv_line"></div>
            <div class="hrv_num_1">0</div>
            <div class="hrv_text">
              The ratio of low to high frequency components (LF/HF) represents the balance between sympathetic and
              parasympathetic nerves.
            </div>
          </div>
          <div class="hrvcard size2">
            <div class="hrv_title">pNN50 Heart Beat</div>
            <div class="hrv_line"></div>
            <div class="hrv_num_2">0</div>
            <div class="hrv_text">
              pNN50 represents the percentage of total heart beats where
              the difference between adjacent hearts is greater than
              50ms.
            </div>
          </div>
          <div class="hrvcard size2">
            <div class="hrv_title">RMSSD Nervous System Function</div>
            <div class="hrv_line"></div>
            <div class="hrv_num_3">0</div>
            <div class="hrv_text">
              RMSSD represents the difference in length between all
              adjacent cardiac intervals to assess nervous system
              activity.
            </div>
          </div>
          <div class="hrvcard size1" style="margin-bottom: 4.3vw">
            <div class="hrv_title">SDNN Cardiac Nervous System</div>
            <div class="hrv_line"></div>
            <div class="hrv_num_4">0</div>
            <div class="hrv_text">
              SDNN represents the standard deviation of all cardiac
              intervals and reflects the overall regulatory capacity of
              the cardiac autonomic nervous system.
            </div>
          </div>
        </div>
        <div class="swiper-slide swiper-no-swiping">
          <!--周报-->
          <div class="card_week_1">
            <div class="title-row" style="height: 28%; margin-bottom: 2%">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-week2">0 hr 0 min</div>
            </div>
            <div class="subtext-week">Nightly average</div>
            <div class="card-line-week"></div>
            <!-- <div class="lab">Total hours</div> -->
            <div class="deep-week stage-row adjust">
              <div class="deep-dot"></div>
              <div class="deep-text">Deep</div>
              <div class="deep-data_2">0 hr 0 min</div>
            </div>
            <div class="light stage-row adjust">
              <div class="light-dot" style="background: #50AFCF"></div>
              <div class="light-text">Light</div>
              <div class="light-data_2">0 hr 0 min</div>
            </div>
            <div class="awake stage-row adjust">
              <div class="awake-dot" style="background: #57A3A3"></div>
              <div class="awake-text">Awake</div>
              <div class="awake-data_2">0 hr 0 min</div>
            </div>
          </div>
          <div class="card2_2" style="display: none; height: 39.7vw">
            <div class="title-row2">
              <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
              <div class="title-text-week2" id="title-text-day2" style="top: 40%">
                0 hr 0 min
              </div>
            </div>
            <div class="subtext-week" style="top: 34%">Nightly average</div>
            <!-- <div class="card-line2"></div>
            <div class="deep stage-row">
              <div class="deep-data" id="deep-data2">0 hr 0 min</div>
            </div> -->
          </div>
          <div class="card3_1" style="height: 23.7vw">
            <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
            <div class="title-text-week2" id="title-text-day">0 hr 0 min</div>
            <div class="subtext-week" style="top: 60%">Nightly average</div>
          </div>
          <div class="card_2">
            <div class="chart-title" id="weeklab">
              Nightly Averages, 4/13 - 4/19
            </div>
            <div class="chart" id="chart_week_total"></div>
            <div class="chart" id="chart_week"></div>
          </div>
          <div class="snoretips_card" id="snoretips_card" style="display: block">
            <div class="top-row">
              <img class="snore_img" src="image/snore_img.png" />
              <div class="top-text" id="top-text2">2 auto adjustments</div>
            </div>
            <div class="card-line5"></div>
            <div class="tips-content">
              Your bed automatically adjusted to the Dawn House pre-programmed Anti Snore position. This helps open your
              lungs and airway, resulting in calmer, deeper breathing.
            </div>
          </div>
          <div class="card_3">
            <div class="detail-title">
              Reaching all of the stages of the sleep cycle is crucial to your
              health and well-being.
            </div>
            <div class="detail-content">
              The cycles include, Light Sleep, which happens no matter how
              much sleep you get, Deep Sleep, which in many ways is more
              restorative. Together the stages make up the sleep cycle which
              works to heal and restore the body.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">LF/HF Nervous System Function</div>
            <div class="hrv_week_num1">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="lfhf"></div>
            <div class="hrv_week_tips">
              The ratio of low to high frequency components (LF/HF) represents
              the balance between sympathetic and parasympathetic nerves.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">pNN50 Heart Beat</div>
            <div class="hrv_week_num2">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="pnn50"></div>
            <div class="hrv_week_tips">
              pNN50 represents the percentage of total heart beats where the
              difference between adjacent hearts is greater than 50ms.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">RMSSD Nervous System Function</div>
            <div class="hrv_week_num3">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="rmssd"></div>
            <div class="hrv_week_tips">
              RMSSD represents the difference in length between all adjacent
              cardiac intervals to assess nervous system activity.
            </div>
          </div>
          <div class="hrvcard size3" style="margin-bottom: 4.3vw">
            <div class="hrv_week_title">SDNN Cardiac Nervous System</div>
            <div class="hrv_week_num4">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="sdnn"></div>
            <div class="hrv_week_tips">
              SDNN represents the standard deviation of all cardiac intervals
              and reflects the overall regulatory capacity of the cardiac
              autonomic nervous system.
            </div>
          </div>
        </div>
        <div class="swiper-slide swiper-no-swiping">
          <!--月报-->
          <div class="card_week_1">
            <div class="title-row" style="height: 28%; margin-bottom: 2%">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-week3">0 hr 0 min</div>
            </div>
            <div class="subtext-week">Nightly average</div>
            <div class="card-line-week"></div>
            <!-- <div class="lab">Total hours</div> -->
            <div class="deep-week stage-row adjust">
              <div class="deep-dot"></div>
              <div class="deep-text">Deep</div>
              <div class="deep-data3">0 hr 0 min</div>
            </div>
            <div class="light stage-row adjust">
              <div class="light-dot" style="background: #50AFCF"></div>
              <div class="light-text">Light</div>
              <div class="light-data3">0 hr 0 min</div>
            </div>
            <div class="awake stage-row adjust">
              <div class="awake-dot" style="background: #57A3A3"></div>
              <div class="awake-text">Awake</div>
              <div class="awake-data3">0 hr 55 min</div>
            </div>
          </div>
          <div class="card2_2" style="display: none; height: 39.7vw">
            <div class="title-row2">
              <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
              <div class="title-text-week2" id="title-text-day3" style="top: 40%">
                0 hr 0 min
              </div>
            </div>
            <div class="subtext-week" style="top: 34%">Nightly average</div>
            <div class="card-line2"></div>
            <div class="deep stage-row">
              <div class="deep-data" id="deep-data3">0 hr 0 min</div>
            </div>
          </div>
          <div class="card3_1" style="height: 23.7vw">
            <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
            <div class="title-text-week3" id="title-text-day">0 hr 0 min</div>
            <div class="subtext-week" style="top: 60%">Nightly average</div>
          </div>
          <div class="card_2">
            <div class="chart-title" id="monthlab">
              Nightly Averages, 4/13 - 4/19
            </div>
            <div class="chart" id="chart_month_total"></div>
            <div class="chart" id="chart_month"></div>
          </div>
          <div class="snoretips_card" id="snoretips_card" style="display: block">
            <div class="top-row">
              <img class="snore_img" src="image/snore_img.png" />
              <div class="top-text" id="top-text3">2 auto adjustments</div>
            </div>
            <div class="card-line5"></div>
            <div class="tips-content">
              Your bed automatically adjusted to the Dawn House pre-programmed Anti Snore position. This helps open your
              lungs and airway, resulting in calmer, deeper breathing.
            </div>
          </div>
          <div class="card_3">
            <div class="detail-title">
              Reaching all of the stages of the sleep cycle is crucial to your
              health and well-being.
            </div>
            <div class="detail-content">
              The cycles include, Light Sleep, which happens no matter how
              much sleep you get, Deep Sleep, which in many ways is more
              restorative. Together the stages make up the sleep cycle which
              works to heal and restore the body.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">LF/HF Nervous System Function</div>
            <div class="hrv_month_num1">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="m_lfhf"></div>
            <div class="hrv_week_tips">
              The ratio of low to high frequency components (LF/HF) represents
              the balance between sympathetic and parasympathetic nerves.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">pNN50 Heart Beat</div>
            <div class="hrv_month_num2">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="m_pnn50"></div>
            <div class="hrv_week_tips">
              pNN50 represents the percentage of total heart beats where the
              difference between adjacent hearts is greater than 50ms.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">RMSSD Nervous System Function</div>
            <div class="hrv_month_num3">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="m_rmssd"></div>
            <div class="hrv_week_tips">
              RMSSD represents the difference in length between all adjacent
              cardiac intervals to assess nervous system activity.
            </div>
          </div>
          <div class="hrvcard size3" style="margin-bottom: 4.3vw">
            <div class="hrv_week_title">SDNN Cardiac Nervous System</div>
            <div class="hrv_month_num4">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="m_sdnn"></div>
            <div class="hrv_week_tips">
              SDNN represents the standard deviation of all cardiac intervals
              and reflects the overall regulatory capacity of the cardiac
              autonomic nervous system.
            </div>
          </div>
        </div>
        <div class="swiper-slide swiper-no-swiping">
          <!--年报-->
          <div class="card_week_1">
            <div class="title-row" style="height: 28%; margin-bottom: 2%">
              <img class="img title-img" src="image/sleep_stage.png" />
              <div class="title-text-week3">0 hr 0 min</div>
            </div>
            <div class="subtext-week">Nightly average</div>
            <div class="card-line-week"></div>
            <!-- <div class="lab">Total hours</div> -->
            <div class="deep-week stage-row adjust">
              <div class="deep-dot"></div>
              <div class="deep-text">Deep</div>
              <div class="deep-data3">0 hr 0 min</div>
            </div>
            <div class="light stage-row adjust">
              <div class="light-dot" style="background: #50AFCF"></div>
              <div class="light-text">Light</div>
              <div class="light-data3">0 hr 0 min</div>
            </div>
            <div class="awake stage-row adjust">
              <div class="awake-dot" style="background: #57A3A3"></div>
              <div class="awake-text">Awake</div>
              <div class="awake-data3">0 hr 55 min</div>
            </div>
          </div>
          <div class="card2_2" style="display: none; height: 39.7vw">
            <div class="title-row2">
              <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
              <div class="title-text-week2" id="title-text-day3" style="top: 40%">
                0 hr 0 min
              </div>
            </div>
            <div class="subtext-week" style="top: 34%">Nightly average</div>
            <div class="card-line2"></div>
            <div class="deep stage-row">
              <div class="deep-data" id="deep-data3">0 hr 0 min</div>
            </div>
          </div>
          <div class="card3_1" style="height: 23.7vw">
            <img class="img title-img" style="top: 40%" src="image/sleep_stage.png" />
            <div class="title-text-week3" id="title-text-day">0 hr 0 min</div>
            <div class="subtext-week" style="top: 60%">Nightly average</div>
          </div>
          <div class="card_2">
            <div class="chart-title" id="yearlab">
              Nightly Averages, 4/13 - 4/19
            </div>
            <div class="chart" id="chart_year_total"></div>
            <div class="chart" id="chart_year"></div>
          </div>
          <div class="snoretips_card" id="snoretips_card" style="display: block">
            <div class="top-row">
              <img class="snore_img" src="image/snore_img.png" />
              <div class="top-text" id="top-text4">0 auto adjustments</div>
            </div>
            <div class="card-line5"></div>
            <div class="tips-content">
              Your bed automatically adjusted to the Dawn House pre-programmed Anti Snore position. This helps open your
              lungs and airway, resulting in calmer, deeper breathing.
            </div>
          </div>
          <div class="card_3">
            <div class="detail-title">
              Reaching all of the stages of the sleep cycle is crucial to your
              health and well-being.
            </div>
            <div class="detail-content">
              The cycles include, Light Sleep, which happens no matter how
              much sleep you get, Deep Sleep, which in many ways is more
              restorative. Together the stages make up the sleep cycle which
              works to heal and restore the body.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">LF/HF Nervous System Function</div>
            <div class="hrv_month_num1">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="y_lfhf"></div>
            <div class="hrv_week_tips">
              The ratio of low to high frequency components (LF/HF) represents
              the balance between sympathetic and parasympathetic nerves.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">pNN50 Heart Beat</div>
            <div class="hrv_month_num2">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="y_pnn50"></div>
            <div class="hrv_week_tips">
              pNN50 represents the percentage of total heart beats where the
              difference between adjacent hearts is greater than 50ms.
            </div>
          </div>
          <div class="hrvcard size3">
            <div class="hrv_week_title">RMSSD Nervous System Function</div>
            <div class="hrv_month_num3">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="y_rmssd"></div>
            <div class="hrv_week_tips">
              RMSSD represents the difference in length between all adjacent
              cardiac intervals to assess nervous system activity.
            </div>
          </div>
          <div class="hrvcard size3" style="margin-bottom: 4.3vw">
            <div class="hrv_week_title">SDNN Cardiac Nervous System</div>
            <div class="hrv_month_num4">0</div>
            <div class="hrv_week_lab">Nightly average</div>
            <div class="hrv-line-week"></div>
            <div class="hrv_week_daterow">Nightly Averages, 4/13 - 4/19</div>
            <div class="hrv_week_chart" id="y_sdnn"></div>
            <div class="hrv_week_tips">
              SDNN represents the standard deviation of all cardiac intervals
              and reflects the overall regulatory capacity of the cardiac
              autonomic nervous system.
            </div>
          </div>
        </div>
        <div class="swiper-slide"></div>
      </div>
    </div>
  </div>
</body>
<script src="js/echart.js"></script>
<script src="js/jquery.js"></script>
<script src="js/swiper.js"></script>
<script src="js/common.js"></script>
<script src="js/drawreport.js"></script>
<script>
  /** 图表初始化 **/
  var chart_awake = echarts.init(document.getElementById('chart_awake')); //心率，醒来日报公用
  var chart_light = echarts.init(document.getElementById('chart_light'));
  var chart_deep = echarts.init(document.getElementById('chart_deep'));
  var chart_lfhf = echarts.init(document.getElementById('lfhf'));
  var chart_pnn50 = echarts.init(document.getElementById('pnn50'));
  var chart_rmssd = echarts.init(document.getElementById('rmssd'));
  var chart_sdnn = echarts.init(document.getElementById('sdnn'));
  var chart_lfhf2 = echarts.init(document.getElementById('m_lfhf'));
  var chart_pnn502 = echarts.init(document.getElementById('m_pnn50'));
  var chart_rmssd2 = echarts.init(document.getElementById('m_rmssd'));
  var chart_sdnn2 = echarts.init(document.getElementById('m_sdnn'));
  var stage = 'day';
  var awake = [];
  var light = [];
  var deep = [];
  var time = [];
  var collect = [];
  var collect2 = [];//心率呼吸采样时间集
  var tmparr = []
  var sonrelist = [];
  var o;
  fontset();
  var user_id = getQueryVariable('user_id'); //用户id入参
  var date = getQueryVariable('date') + ' 00:00:00'; //日期入参
  var token = getQueryVariable('token'); //token 入参
  var model = getQueryVariable('model'); //模块入参
  var host = getQueryVariable('host'); //动态路径
  var share = getQueryVariable('share'); //报告分享标志位
  var timetype = getQueryVariable('timetype');//24h 12h标志位
  var toptitle = ""
  var MonthArr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Spt", "Oct", "Nov", "Dec"]
  var modelList = ['total', 'deep', 'light', 'heart', 'breath', 'snore', 'move', 'hrv']
  var modelList2 = ['Total Sleep', 'Deep Sleep', 'Light Sleep', 'Heart Rate', 'Respiratory Rate', 'Snoring', 'Movement', 'Heart Rate Variability']
  let temp = new Date(date.replace(/-/g, '/'))
  let temp2 = dateformat(new Date(), 0, 2)
  let yn = temp.getFullYear()
  let mn = temp.getMonth()
  let dn = temp.getDate()
  $('#time-toptext').html(MonthArr[mn] + ' ' + dn + ', ' + yn)

  var mySwiper
  contentChange(model)
  drawchart(stage);
  initialization();


  /** Slide滑动初始化 **/
  setTimeout(() => {
    mySwiper = new Swiper('.swiper-container', {
      autoplay: false, //可选选项，自动滑动
      autoHeight: true,
      on: {
        slideChange: function () {
          // alert(this.activeIndex);//切换结束时，告诉我现在是第几个slide
          movecheck(this.activeIndex)
        },
      },
    });
  }, 300)
  //页面初始化
  function initialization () {
    let winWidth = window.innerWidth;
    let height2_2 = '';
    let height_3 = '';
    if (share == 1) {  //判断是否为别人分享的报告
      $('#title-row').css({ display: 'block' })
      let a = modelList.indexOf(model)
      toptitle = capitalize(modelList2[a])
      $('#title-row').html(toptitle)
    } else {
      $('#title-row').css({ display: 'none' })
      $('#title-row').html("")
    }
    if (model == 'deep') {
      $('.time-row').css({ display: 'block' })
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.hrvcard').css({ display: 'none' })
      $('.title-text-week2').css({ top: '40%' });
      $('.title-img').attr('src', 'image/deep_sleep.png');
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'block' });
      $('.card_week_1').css({ display: 'none' });
      $('#chart_awake').css({ display: 'block', height: '25%' });
      $('#chart_light').css({ display: 'block' });
      $('#chart_deep').css({ display: 'block' });
      $('.card2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'none' });
      $('#deep-data').html('0% of your evening rest');
      $('.snoretips_card').css({ display: 'none' })
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      // $('.card_3').css({ height: '324px' });
      if (winWidth >= 768) {
        $('.card_3').css({ height: '25.1vw' });
        $('.card2_2').css({ height: '17.9vw' });
        $('.detail-title').css('cssText', 'line-height:22px');
        $('.detail-content').css('cssText', 'line-height:22px');
      }
    } else if (model == 'light') {
      $('.time-row').css({ display: 'block' })
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.hrvcard').css({ display: 'none' })
      $('.title-text-week2').css({ top: '40%' });
      $('.title-img').attr('src', 'image/light_sleep.png');
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'block' });
      $('#chart_awake').css({ display: 'block', height: '25%' });
      $('#chart_light').css({ display: 'block' });
      $('#chart_deep').css({ display: 'block' });
      $('.card2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'none' });
      $('.card_week_1').css({ display: 'none' });
      $('#deep-data').html('0% of your evening rest');
      $('.snoretips_card').css({ display: 'none' })
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      if (winWidth >= 768) {
        $('.card_3').css({ height: '22.1vw' });
        $('.card2_2').css({ height: '17.9vw' });
        $('.detail-title').css({ 'margin-top': '4%' });
      } else {
        // $('.card_3').css({ height: '255px' });
      }
    } else if (model == 'hrv') {
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.hrvcard').css({ display: 'none' })
      $('.title-img').attr('src', 'image/hrv.png');
      $('.card_week_1').css({ display: 'none' });
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('.card_2').css({ display: 'none' });
      $('#chart_awake').css({ display: 'block', height: '90%' });
      $('#chart_light').css({ display: 'none' });
      $('#chart_deep').css({ display: 'none' });
      $('.snoretips_card').css({ display: 'none' })
      // $('.text-adjust').css({ left: '4.7% !important' });
      $('.card3_1').css({ display: 'block' });
      $('.deep-data').html('0 bpm avg');
      $('.deep-data2').html('0 bpm avg');
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      if (winWidth >= 768) {
        $('.card_3').css({ height: '24.1vw' });
        $('.card3_1').css({ height: '17.9vw' });
        $('.detail-title').css({ 'margin-top': '4%' });
        $('.subtext-week').css('cssText', 'top: 45% !important ');
      }
    } else if (model == 'heart') {
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.hrvcard').css({ display: 'none' })
      $('.title-img').attr('src', 'image/heart_rate.png');
      $('.card_week_1').css({ display: 'none' });
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('.card_2').css({ display: 'block' });
      $('#chart_awake').css({ display: 'block', height: '90%' });
      $('#chart_light').css({ display: 'none' });
      $('#chart_deep').css({ display: 'none' });
      $('.snoretips_card').css({ display: 'none' })
      $('.time-row').css({ display: 'none' })
      // $('.card_3').css({ height: '301px' });
      // $('.card_2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'block' });
      $('.deep-data').html('0 bpm avg');
      $('.deep-data2').html('0 bpm avg');
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      if (winWidth >= 768) {
        $('.card_3').css({ height: '24.1vw' });
        $('.card3_1').css({ height: '17.9vw' });
        $('.detail-title').css({ 'margin-top': '4%' });
        $('.subtext-week').css('cssText', 'top: 45% !important ');
      }
    } else if (model == 'breath') {
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.hrvcard').css({ display: 'none' })
      $('.card_week_1').css({ display: 'none' });
      $('.title-img').attr('src', 'image/breath.png');
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'block' });
      $('.card_2').css({ display: 'block' });
      $('#chart_awake').css({ display: 'block', height: '90%' });
      $('#chart_light').css({ display: 'none' });
      $('#chart_deep').css({ display: 'none' });
      $('.snoretips_card').css({ display: 'none' })
      $('.deep-data').html('0 average');
      $('.deep-data2').html('0 average');
      if (winWidth >= 768) {
        $('.card_3').css({ height: '22.1vw' });
        $('.card3_1').css({ height: '17.9vw' });
        $('.detail-title').css({ 'margin-top': '4%' });
        $('.subtext-week').css('cssText', 'top: 45% !important;');
      } else {
        // $('.card_3').css({ height: '232px' });
      }
    } else if (model == 'snore') {
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      $('.card_week_1').css({ display: 'none' });
      $('.title-img').attr('src', 'image/snoring.png');
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('#chart_awake').css({ display: 'block', height: '90%' });
      $('#chart_light').css({ display: 'none' });
      $('#chart_deep').css({ display: 'none' });
      $('.card3_1').css({ display: 'block' });
      $('.card4_1').css({ display: 'none' });
      $('.card_2').css({ display: 'none' });
      $('.hrvcard').css({ display: 'none' })
      $('.card_3').css({ display: 'block' });
      for (let i = 0; i < sonrelist.length; i++) {
        $('#snore-row').append(
          "<div class='row'><div class='snore_time'>" +
          sonrelist[i].time +
          "</div><div class='time_length'>" +
          sonrelist[i].length +
          '</div></div>'
        );
      }
      if (winWidth >= 768) {
        // $('.card_3').css({ height: '22.1vw' });
        $('.card_chart').css(
          'cssText',
          'margin-top: 6% !important;height:90%'
        );
        $('.snoretips_card').css(
          'cssText',
          'width: 65.9%;height: 23.3vw;border-radius: 24px;box-shadow: 0px 2px 6px 0px undefined; margin-top: 2%;'
        );
        $('.detail-title').css({ 'margin-top': '4%' });
        $('.subtext-week').css('cssText', 'top: 45% !important;');
        $('.card_3').css({ height: '24vw' });
      }
      $('#snoretips_card').css({ display: 'block' });
    } else if (model == 'move') {
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      $('.card_week_1').css({ display: 'none' });
      if (model == 'move') {
        $('.title-img').attr('src', 'image/movement.png');
      } else {
        $('.title-img').attr('src', 'image/outbed.png');
      }
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'block' });
      $('.card4_1').css({ display: 'none' });
      $('.card_2').css({ display: 'none' });
      $('.card5_1').css({ display: 'none' });
      $('.card_3').css({ display: 'block' });
      $('.hrvcard').css({ display: 'none' })
      $('.snoretips_card').css({ display: 'none' })
      if (winWidth >= 768) {
        // $('.card_3').css({ height: '22.1vw' });
        $('.card_chart').css(
          'cssText',
          'margin-top: 6% !important;height:90%'
        );
        // $('.snoretips_card').css(
        //   'cssText',
        //   'width: 65.9%;height: 23.3vw;border-radius: 24px;box-shadow: 0px 2px 6px 0px undefined; margin-top: 2%;'
        // );
        $('.detail-title').css({ 'margin-top': '4%' });
        $('.subtext-week').css('cssText', 'top: 45% !important;');
        $('.card_3').css({ height: '26vw' });
      }
    } else if (model == 'outbed') {
      $('#chart_week_total').css({ display: 'none' })
      $('#chart_week').css({ display: 'block' })
      $('#chart_month_total').css({ display: 'none' })
      $('#chart_month').css({ display: 'block' })
      $('#chart_year_total').css({ display: 'none' })
      $('#chart_year').css({ display: 'block' })
      $('.title-text-week3').css('cssText', 'top: 40% !important ');
      $('.card_week_1').css({ display: 'none' });
      if (model == 'move') {
        $('.title-img').attr('src', 'image/movement.png');
      } else {
        $('.title-img').attr('src', 'image/outbed.png');
      }
      $('.card_1').css({ display: 'none' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'block' });
      $('.card4_1').css({ display: 'none' });
      $('.card_2').css({ display: 'none' });
      $('.card5_1').css({ display: 'none' });
      $('.card_3').css({ display: 'block' });
      $('.hrvcard').css({ display: 'none' })
      $('.snoretips_card').css({ display: 'none' })
      if (winWidth >= 768) {
        // $('.card_3').css({ height: '22.1vw' });
        $('.card_chart').css(
          'cssText',
          'margin-top: 6% !important;height:90%'
        );
        // $('.snoretips_card').css(
        //   'cssText',
        //   'width: 65.9%;height: 23.3vw;border-radius: 24px;box-shadow: 0px 2px 6px 0px undefined; margin-top: 2%;'
        // );
        $('.detail-title').css({ 'margin-top': '4%' });
        $('.subtext-week').css('cssText', 'top: 45% !important;');
        $('.card_3').css({ height: '26vw' });
      }
    }
    else {
      $('.time-row').css({ display: 'block' })
      $('.title-img').attr('src', 'image/sleep_stage.png');
      $('#chart_week_total').css({ display: 'block' })
      $('#chart_week').css({ display: 'none' })
      $('#chart_month_total').css({ display: 'block' })
      $('#chart_month').css({ display: 'none' })
      $('#chart_year_total').css({ display: 'block' })
      $('#chart_year').css({ display: 'none' })
      $('.card_1').css({ display: 'block' });
      $('.card2_1').css({ display: 'none' });
      $('.card2_2').css({ display: 'none' });
      $('.card3_1').css({ display: 'none' });
      $('.deep-data').removeClass('text-adjust');
      $('.title-text-week2').css({ top: '50%' });
      $('.hrvcard').css({ display: 'none' })
      $('.snoretips_card').css({ display: 'none' })
      $('.card_week_1').css({ display: 'block' });
      $('.title-text-week3').css('cssText', 'top: 50% !important ');
      // $('.card_3').css({ height: '278px' });
      if (winWidth >= 768) {
        $('.subtext-week').css('cssText', 'top: 18% !important;');
        $('.card_3').css('cssText', 'height:23.3vw');
        $('.detail-title').css('cssText', 'line-height:22px');
        $('.detail-content').css('cssText', 'line-height:22px');
      }
    }
  }
  /** Tabs选项卡点击方法 **/
  $('#day').click(function () {
    $('#day').addClass('active');
    $('#line_1').css({ display: 'none' });
    $('#line_2').css({ display: 'block' });
    $('#line_3').css({ display: 'block' });
    $('#week').removeClass('active');
    $('#month').removeClass('active');
    $('#year').removeClass('active');
    mySwiper.slideTo(0, 500, false);
    if (model == 'move') {
      $('.card_2').css({ display: 'none' });
    }
    stage = 'day';
    drawchart(stage);
  });
  $('#week').click(function () {
    $('#week').addClass('active');
    $('#line_1').css({ display: 'none' });
    $('#line_2').css({ display: 'none' });
    $('#line_3').css({ display: 'block' });
    $('#day').removeClass('active');
    $('#month').removeClass('active');
    $('#year').removeClass('active');
    mySwiper.slideTo(1, 500, false);
    stage = 'week';
    drawchart(stage);
  });
  $('#month').click(function () {
    $('#month').addClass('active');
    $('#line_1').css({ display: 'block' });
    $('#line_2').css({ display: 'none' });
    $('#line_2').css({ display: 'none' });
    $('#day').removeClass('active');
    $('#week').removeClass('active');
    $('#year').removeClass('active');
    mySwiper.slideTo(2, 500, false);
    stage = 'month';
    drawchart(stage);
  });
  $('#year').click(function () {
    $('#year').addClass('active');
    $('#line_1').css({ display: 'block' });
    $('#line_2').css({ display: 'block' });
    $('#line_3').css({ display: 'none' });
    $('#day').removeClass('active');
    $('#week').removeClass('active');
    $('#month').removeClass('active');
    mySwiper.slideTo(3, 500, false);
    stage = 'year';
    drawchart(stage);
  });
  //左右滑动判断
  $(".swiper-container").on("touchstart", function (e) {
    // e.preventDefault();
    startX = e.originalEvent.changedTouches[0].pageX;
    startY = e.originalEvent.changedTouches[0].pageY;
  });
  $(".swiper-container").on("touchend", function (e) {
    // e.preventDefault();
    moveEndX = e.originalEvent.changedTouches[0].pageX;
    moveEndY = e.originalEvent.changedTouches[0].pageY,
      X = moveEndX - startX;
    Y = moveEndY - startY;
    if (Math.abs(X) > 100 && X > 0) {  //从左往右滑
      let a = modelList.indexOf(model)
      if (a > 0) {
        model = modelList[a - 1]
        toptitle = capitalize(modelList2[a - 1])
        $('title').html(toptitle)
        initialization()
        console.log('model0', model)
        $('#day').addClass('active');
        $('#line_1').css({ display: 'none' });
        $('#line_2').css({ display: 'block' });
        $('#week').removeClass('active');
        $('#month').removeClass('active');
        $('#year').removeClass('active');
        mySwiper.slideTo(0, 500, false);
        stage = 'day';
        drawchart(stage);
      } else {
      }
      // console.log("left 2 right");
    }
    if (Math.abs(X) > 100 && X < 0) {
      let a = modelList.indexOf(model)
      if (a >= 8) {
      } else {
        model = modelList[a + 1]
        toptitle = capitalize(modelList2[a + 1])
        $('title').html(toptitle)
        initialization()
        $('#day').addClass('active');
        $('#line_1').css({ display: 'none' });
        $('#line_2').css({ display: 'block' });
        $('#week').removeClass('active');
        $('#month').removeClass('active');
        $('#year').removeClass('active');
        mySwiper.slideTo(0, 500, false);
        stage = 'day';
        drawchart(stage)
      }
      // console.log("right 2 left");
    }
  });
  /** 图表绘制 **/
  function drawchart (stage) {
    if (stage == 'day') {
      getdayreport();
      // timeformat(date, stage);
    }
    if (stage == 'week') {
      getweekreport();
      // timeformat(date, stage);
    }
    if (stage == 'month') {
      getmonthreport();
    }
    if (stage == 'year') {
      getyearreport();
    }
  }

  //日报
  function getdayreport () {
    var tmparr = []
    let url = '';
    if (host == 1) {
      url =
        'https://resthomeus.smartbed.top/ergoZone/api/v1/sleep/getSleepDay';
    } else {
      url =
        'https://resthomeus.smartbed.top/ergoZone/api/v1/sleep/getSleepDay';
    }
    $('#time-toptext').html(MonthArr[mn] + ' ' + dn + ', ' + yn)
    $.ajax({
      url: url,
      type: 'GET',
      data: {
        user_id: user_id,
        date: date.substring(0, 10),
      },
      headers: { Authorization: token },
      datatype: 'application/json',
      success: function (res) {
        if (res.code == 10000) {
          showduration(
            res.data.sleep_duration,
            res.data.deep_sleep_duration,
            res.data.shallow_sleep_duration,
            res.data.clear_duration + res.data.left_bed_duration
          );
          getcollect(res.data);
          sleep_x_handler(res.data.sleep_stage)
          if (model == 'heart' || model == 'breath' || model == 'hrv') {
            $('.subtext-week').html('24 hour average')
          } else {
            if (timetype == 12) {
              $('.subtext-week').html(dateformat(res.data.sleep_time.replace(/-/g, '/'), 0, 3) + ' - ' + dateformat(res.data.wake_time.replace(/-/g, '/'), 0, 3))
            } else {
              $('.subtext-week').html(dateformat(res.data.sleep_time.replace(/-/g, '/'), 0, 4) + ' - ' + dateformat(res.data.wake_time.replace(/-/g, '/'), 0, 4))
            }
          }



          if (timetype == 12) {
            $('#left-time').html(
              dateformat(res.data.sleep_time.replace(/-/g, '/'), 0, 3)
            );
            $('#right-time').html(
              dateformat(res.data.wake_time.replace(/-/g, '/'), 0, 3)
            );
          } else {
            $('#left-time').html(
              dateformat(res.data.sleep_time.replace(/-/g, '/'), 0, 4)
            );
            $('#right-time').html(
              dateformat(res.data.wake_time.replace(/-/g, '/'), 0, 4)
            );
          }
          if (model == 'total') {
            console.log('deep', deep)
            drawdaychart1(chart_awake, awake, 3);
            drawdaychart2(chart_light, light, 2);
            drawdaychart3(chart_deep, deep, 1);
            $('.detail-title').html(
              'Reaching all of the stages of the sleep cycle is crucial to your health and well-being.'
            );
            $('.detail-content').html(
              "The stages include light and deep sleep. Light sleep is periods when it's easier to wake up while deep sleep is in many ways more restorative. Together the stages make up the sleep cycle which works to heal and restore the body."
            );
          } else if (model == 'deep') {
            awake = [];
            light = [];
            let a =
              Math.round(
                (res.data.deep_sleep_duration * 100) / res.data.sleep_duration
              ) + '% of your evening rest';
            if (res.data.deep_sleep_duration > 60) {
              let b = Math.floor(res.data.deep_sleep_duration / 60);
              let c = res.data.deep_sleep_duration % 60;
              $('.title-text-day').html(b + ' hr ' + c + ' min');
            } else {
              let c = res.data.deep_sleep_duration % 60;
              $('.title-text-day').html(0 + ' hr ' + c + ' min');
            }
            $('#deep-data0').html(a);
            drawdaychart1(chart_awake, awake, 3);
            drawdaychart2(chart_light, light, 2);
            drawdaychart3(chart_deep, deep, 1);
            $('.detail-title').html(
              'The average healthy adult gets roughly 1 to 2 hours of deep sleep per 8 hours of nightly sleep.'
            );
            $('.detail-content').html(
              "Deep Sleep is when the body reaches a point of relaxation, when the body is healing and restoring energy, the immune system is energized, and the brain goes through the necessary exercises to consolidate memory, learn and process emotions. It's is the 4th Stage of the sleep cycle."
            );
          } else if (model == 'light') {
            awake = [];
            deep = o;
            let a =
              Math.round(
                (res.data.shallow_sleep_duration * 100) /
                res.data.sleep_duration
              ) + '% of your evening rest';
            if (res.data.shallow_sleep_duration > 60) {
              let b = Math.floor(res.data.shallow_sleep_duration / 60);
              let c = res.data.shallow_sleep_duration % 60;
              $('.title-text-day').html(b + ' hr ' + c + ' min');
            } else {
              let c = res.data.shallow_sleep_duration % 60;
              $('.title-text-day').html(0 + ' hr ' + c + ' min');
            }
            $('#deep-data0').html(a);
            console.log('deep', deep)
            drawdaychart1(chart_awake, awake, 3);
            drawdaychart2(chart_light, light, 2);
            drawdaychart3(chart_deep, deep, 1);
            $('.detail-title').html(
              'Light sleep initiates your sleep cycle and acts as a transition to deeper sleep stages.'
            );
            $('.detail-content').html(
              'During this stage your muscles begin to relax, your heart rate and breathing slow down, and you wake up easily. During light sleep, you can expect the following: muscles relax and may jerk.'
            );
          } else if (model == 'heart') {
            let heart = JSON.parse(res.data.heart_rate_stage);
            let max = Math.max(...heart);
            let min = Math.min(...heart);
            let interval = Math.round(collect.length / 5);
            heart.unshift(heart[0]);
            $('.title-text-week1').html(res.data.avg_heart_rate + ' bpm');
            $('.detail-title').html(
              "Your resting heart rate is the number of times your heart beats per minute when you're at rest. "
            );
            $('.detail-content').html(
              "A good time to check it is in the morning after you've had a good night's sleep. For most of us, between 60 and 100 beats per minute (bpm) is normal. The rate can be affected by factors like stress, anxiety, hormones, medication, and how physically active you are."
            );
            drawheartchart(chart_awake, heart, collect2, max, min, interval);
          } else if (model == 'breath') {
            let breath = JSON.parse(res.data.breath_rate_stage);
            let max = Math.max(...breath);
            breath.unshift(breath[0]);
            $('.title-text-week1').html(
              res.data.avg_breath_rate + ' breaths/min'
            );
            $('.detail-title').html(
              'Respiratory rate, or breathing rate, is the number of breaths you take per minute.'
            );
            $('.detail-content').html(
              'A typical respiratory rate for healthy adults is 12–20 breaths per minute. Tracking your respiratory rate trends over time allows you to spot an abnormally high or low rate.'
            );
            drawbreathchart(chart_awake, breath, collect2, max)
          } else if (model == 'move') {
            $('.title-text-week1').html(res.data.twitch_times + (res.data.twitch_times == 1 ? ' movement' : ' movements'));
            $('.detail-title').html(
              'In the span of a week, the average person tosses 37 to 40 times each night.'
            );
            $('.detail-content').html(
              'Tracking these movements will allow you to understand your sleep patterns. Fragmented or interrupted sleep can lead to daytime consequences, such as fatigue and trouble concentrating the next day. Understanding these conditions is a helpful first step to finding relief.'
            );
          } else if (model == 'hrv') {
            $('.title-text-week1').html(res.data.rmssd + ' ms');
            $('.detail-title').html(
              'HRV measures the time variation between successive heartbeats. '
            );
            $('.detail-content').html(
              "Keeping track of your heart rate variability metrics can provide insights, such as understanding how outside factors could be having a negative impact. Steadily rising numbers will indicate progress in your fitness, while a decline from your normal averages may highlight a need to address an underlying issue such as sleep or stress."
            );
          } else if (model == 'snore') {
            let color = 'rgba(38,186,171,1)';
            $('.title-text-week1').html(res.data.snore_times + (res.data.snore_times == 1 ? ' time' : ' times'));
            $('#top-text1').html(
              res.data.anti_snore_times + (res.data.anti_snore_times == 1 ? ' Auto Adjustment' : ' Auto Adjustments'));
            $('.detail-title').html(
              'Habitual snoring can lead to disturbed sleep or be a sign of other complications.'
            );
            $('.detail-content').html(
              "Tracking your snoring activity can help inform needed next steps. Snoring can often be reduced with lifestyle changes to include raising the head of your bed. For this to work, it's important to raise the whole mattress and not just use more pillows."
            );
            let snore = [];
            snore = snorehandle(
              res.data.sleep_time,
              res.data.wake_time,
              res.data.anti_snore_stage,
              snore
            );
            drawsnorechart(chart_awake, snore, color);
          } else if (model == 'outbed') {
            let temp = res.data.left_bed_time
            if (temp != 1) {
              $('.title-text-week1').html(res.data.left_bed_time + ' times');
            } else {
              $('.title-text-week1').html(res.data.left_bed_time + ' time');
            }
            $('.detail-title').html(
              'Track the number of times you get out of bed at night to monitor changes in your health. '
            );
            $('.detail-content').html(
              "Increased frequency may be caused by: restless leg syndrome, sleep apnea, increased urination, medication, sleep disorders, pain, or other issues.  Getting out of bed at night can lead to increased fall risks. If you notice a change in this health metric you may want to contact your physician."
            );
          }
        }
      },
      failed: function (res) {
        console.log(res);
      },
    });
  }
  //周报
  function getweekreport () {
    let arr = [];
    let arr2 = [];
    let arr3 = [];
    let arr4 = [];
    let startdate = dateformat(
      new Date(date.replace(/-/g, '/')).getTime() - 6 * 24 * 60 * 60 * 1000,
      0,
      2
    ).substring(0, 10);
    let url = '';
    if (host == 1) {
      url =
        'https://wellness.ergomotion.com/ergoZone/api/v1/sleep/getSleepWeek';
    } else {
      url =
        'https://resthomeus.smartbed.top/ergoZone/api/v1/sleep/getSleepWeek';
    }
    let temp1 = new Date(startdate.replace(/-/g, '/') + ' 00:00:00')
    let yn1 = temp1.getFullYear()
    let mn1 = temp1.getMonth()
    let dn1 = temp1.getDate()
    if (yn1 != yn) {
      $('#time-toptext').html(MonthArr[mn1] + ' ' + dn1 + ', ' + yn1 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
    } else {
      $('#time-toptext').html(MonthArr[mn1] + ' ' + dn1 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
    }
    $.ajax({
      url: url,
      type: 'get',
      data: {
        user_id: user_id,
        start_date: startdate,
        end_date: date.substring(0, 10),
      },
      headers: {
        Authorization: token,
      },
      datatype: 'application/json',
      success: function (res) {
        let sum = res.data;
        let avg = 0;
        let avg1 = 0;
        let avg2 = 0;
        let avg3 = 0;
        let days = 0;
        if (res.code == 10000) {
          let a = res.data.sleep_duration_week;
          for (let i = 0; i < a.length; i++) {
            let temp_date = a[i].date.substring(5, 10).split('-')
            let temp_date_1 = parseInt(temp_date[0]).toString()
            let temp_date_2 = parseInt(temp_date[1]).toString()
            arr.push((temp_date_1 + '/' + temp_date_2));
            arr2.push(a[i].deep_sleep_duration);
            arr3.push(a[i].shallow_sleep_duration);
            arr4.push(a[i].clear_duration);
            if (
              a[i].deep_sleep_duration +
              a[i].shallow_sleep_duration +
              a[i].clear_duration !=
              0
            ) {
              days = days + 1;
            }
            avg =
              avg +
              a[i].deep_sleep_duration +
              a[i].shallow_sleep_duration +
              a[i].clear_duration;
            avg1 = avg1 + a[i].deep_sleep_duration;
            avg2 = avg2 + a[i].shallow_sleep_duration;
            avg3 = avg3 + a[i].clear_duration;
          }
          let max;
          let max1 = Math.max(...arr2);
          let max2 = Math.max(...arr3);
          let max3 = Math.max(...arr4);
          if (max1 > max2) {
            if (max1 > max3) {
              max = max1;
            } else {
              max = max3;
            }
          } else {
            if (max2 > max3) {
              max = max2;
            } else {
              max = max3;
            }
          }
          $('#weeklab').html(
            'Nightly Averages, ' + arr[0] + '-' + arr[arr.length - 1]
          );
          $('.subtext-week').html('Nightly average')
          if (model == 'total') {
            var chart_week_total = echarts.init(document.getElementById('chart_week_total'));
            let s_0 = Math.round(avg / days); //大标题平均
            let a1 = Math.round(avg1 / days); //深睡平均
            let a2 = Math.round(avg2 / days); //浅睡平均
            let a3 = Math.round(avg3 / days); //清醒平均
            drawweekchart(chart_week_total, arr2, arr3, arr4, arr, max);
            if (a1 + a2 > 60) {
              let b = Math.floor((a1 + a2) / 60);
              let c = (a1 + a2) % 60;
              $('.title-text-week2').html(b + ' hr ' + c + ' min');
            } else {
              let c = s_0 % 60;
              $('.title-text-week2').html(0 + ' hr ' + c + ' min');
            }
            if (a1 > 60) {
              let b = Math.floor(a1 / 60);
              let c = a1 % 60;
              $('.deep-data_2').html(b + ' hr ' + c + ' min');
            } else {
              let c = a1.deep_sleep_duration % 60;
              $('.deep-data_2').html(0 + ' hr ' + c + ' min');
            }
            if (a2 > 60) {
              let b = Math.floor(a2 / 60);
              let c = a2 % 60;
              $('.light-data_2').html(b + ' hr ' + c + ' min');
            } else {
              let c = a2 % 60;
              $('.light-data_2').html(0 + ' hr ' + c + ' min');
            }
            if (a3 > 60) {
              let b = Math.floor(a3 / 60);
              let c = a3 % 60;

              $('.awake-data_2').html(b + ' hr ' + c + ' min');
            } else {
              let c = a3 % 60;
              $('.awake-data_2').html(0 + ' hr ' + c + ' min');
            }
          } else if (model == 'deep') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let a1 = Math.round(avg1 / days); //深睡平均
            let color = 'rgba(0, 132, 205, 1)';
            let color2 = 'rgba(0, 132, 205, 1)';
            let color3 = 'rgba(0, 132, 205, 0.2)';
            drawmonthchart(chart_week, arr2, arr, color2, color3, color);
            console.log('a1', a1)
            if (a1 > 60) {
              let b = Math.floor(a1 / 60);
              let c = a1 % 60;
              // $('#deep-data2').html(b + ' hr ' + c + ' min');
              $('.title-text-week2').html(b + ' hr ' + c + ' min');
            } else {
              let c = a1 % 60;
              // $('#deep-data2').html(0 + ' hr ' + c + ' min');
              $('.title-text-week2').html(0 + ' hr ' + c + ' min');
            }
            // if(sum.)
          } else if (model == 'light') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let color = 'rgba(80, 175, 207, 1)';
            let color2 = 'rgba(80, 175, 207, 1)';
            let color3 = 'rgba(80, 175, 207, 0.2)';
            drawmonthchart(chart_week, arr3, arr, color2, color3, color);
            let a2 = Math.round(avg2 / days); //浅睡平均
            if (a2 > 60) {
              let b = Math.floor(a2 / 60);
              let c = a2 % 60;
              $('#deep-data2').html(b + ' hr ' + c + ' min');
              $('.title-text-week2').html(b + ' hr ' + c + ' min');
            } else {
              let c = a2 % 60;
              $('#deep-data2').html(0 + ' hr ' + c + ' min');
              $('.title-text-week2').html(0 + ' hr ' + c + ' min');
            }
          } else if (model == 'heart') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let color = 'rgba(233, 73, 95, 1)';
            let color2 = 'rgba(233, 73, 95, 1)';
            let color3 = 'rgba(233, 73, 95, 0.2)';
            let heart = [];
            let avg = 0;
            let days = 0;
            for (let i = 0; i < res.data.sleep_duration_week.length; i++) {
              heart.push(res.data.sleep_duration_week[i].avg_heart_rate);
              if (res.data.sleep_duration_week[i] != 0) {
                avg = res.data.sleep_duration_week[i].avg_heart_rate + avg;
                days = days + 1;
              }
            }
            if (avg != 0) {
              $('.title-text-week2').html(Math.round(avg / days) + ' bpm');
            } else {
              $('.title-text-week2').html('0 bpm');
            }
            // console.log('heart', heart)
            // console.log('arr', arr)
            drawmonthchart(chart_week, heart, arr, color2, color3, color);
          } else if (model == 'breath') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let color = "rgba(73, 165, 222, 1)"
            let color2 = "rgba(73, 165, 222, 1)"
            let color3 = "rgba(73, 165, 222, 0.2)"
            let breath = res.data.breath_rate_stage;
            let avg = 0;
            let days = 0;
            // let temparr = res.data.breath_rate_stage;
            for (let i = 0; i < breath.length; i++) {
              if (breath[i] != 0) {
                avg = breath[i] + avg;
                days = days + 1;
              }
            }
            if (avg != 0) {
              $('.title-text-week2').html(
                Math.round(avg / days) + ' breaths/min'
              );
            } else {
              $('.title-text-week2').html('0 breaths/min');
            }
            drawmonthchart(chart_week, breath, arr, color2, color3, color);
          } else if (model == 'move') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let slm = [];
            let avg = 0;
            let days = 0;
            let arr_1 = res.data.sleep_duration_week;
            let color = 'rgba(138, 134, 226, 1)';
            let color2 = 'rgba(138, 134, 226, 1)';
            let color3 = 'rgba(138, 134, 226, 0.2)';
            for (let i = 0; i < arr_1.length; i++) {
              // if (arr_1[i].twitch_times != 0) {
              avg = arr_1[i].twitch_times + avg;
              slm.push(arr_1[i].twitch_times);
              days = days + 1;
              // }
            }
            if (avg != 0) {
              $('.title-text-week2').html(
                Math.round(avg / days) + (Math.round(avg / days) == 1 ? ' movement' : ' movements')
              );
            } else {
              $('.title-text-week2').html('0 movements');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_week, slm, arr, color2, color3, color);
          } else if (model == 'hrv') {
            let color = 'rgba(243, 193, 83, 1)';
            let color2 = 'rgba(243, 193, 83, 1)';
            let color3 = 'rgba(243, 193, 83, 0.2)';
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let rmssd = res.data.rmssd_stage;
            let avg3 = 0;
            for (let i = 0; i < rmssd.length; i++) {
              if (rmssd[i] != 0) {
                days = days + 1;
              }
              avg3 = avg3 + rmssd[i];
            }
            if (avg3 != 0) {
              $('.title-text-week2').html(
                Math.round(avg3 / days) + ' ms'
              );
            } else {
              $('.title-text-week2').html('0 ms');
            }
            drawmonthchart(chart_week, rmssd, arr, color2, color3, color);
            // drawmonthchart(chart_sdnn, sdnn, arr, color2);
          } else if (model == 'snore') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let color = 'rgba(109, 173, 143, 1)';
            let color2 = 'rgba(109, 173, 143, 1)';
            let color3 = 'rgba(109, 173, 143, 0.2)';
            let snore = [];
            let avg = 0;
            let avg2 = 0;
            let days = 0;
            let sum0 = res.data.snore_times_stage;
            let sum = res.data.sleep_duration_week;
            for (let i = 0; i < sum0.length; i++) {
              days = days + 1;
              snore.push(sum[i].anti_snore_times);
              avg = avg + sum[i].anti_snore_times;
            }
            for (let i = 0; i < sum0.length; i++) {
              avg2 = avg2 + sum0[i];
            }
            if (avg2 != 0) {
              $('.title-text-week2').html(
                Math.round(avg2 / sum0.length) + (Math.round(avg2 / sum0.length) == 1 ? ' time' : ' times')
              );
            } else {
              $('.title-text-week2').html('0 times');
            }

            if (avg != 0) {
              $('#top-text2').html(
                Math.round(avg / days) + ' auto adjustments'
              );
            } else {
              $('#top-text2').html('0 auto adjustments');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_week, snore, arr, color2, color3, color);
          } else if (model == 'outbed') {
            var chart_week = echarts.init(document.getElementById('chart_week'));
            let color = 'rgba(240, 92, 73, 1)'
            let color2 = 'rgba(240, 92, 73, 1)'
            let color3 = 'rgba(240, 92, 73, 0.2)'
            let temp = res.data.left_bed_time
            if (temp != 1) {
              $('.title-text-week2').html(temp + ' times');
            } else {
              $('.title-text-week2').html(temp + ' time');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_week, res.data.left_bed_times, arr, color2, color3, color)
          }
          else {
          }
        }
      },
    });
  }
  //月报
  function getmonthreport () {
    let arr = [];
    let value0 = [];
    let value1 = [];
    let value2 = [];
    let value3 = [];
    let time = dateformat(
      new Date(date.replace(/-/g, '/')).getTime() - 24 * 60 * 60 * 30 * 1000,
      0,
      2
    );
    let url = '';
    if (host == 1) {
      url =
        'https://wellness.ergomotion.com/ergoZone/api/v1/sleep/getSleepMonth';
    } else {
      url =
        'https://resthomeus.smartbed.top/ergoZone/api/v1/sleep/getSleepMonth';
    }
    let temp2 = new Date(time.replace(/-/g, '/') + " 00:00:00")
    let yn2 = temp2.getFullYear()
    let mn2 = temp2.getMonth()
    let dn2 = temp2.getDate()
    if (yn2 != yn) {
      $('#time-toptext').html(MonthArr[mn2] + ' ' + dn2 + ', ' + yn2 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
    } else {
      $('#time-toptext').html(MonthArr[mn2] + ' ' + dn2 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
    }
    $.ajax({
      url: url,
      type: 'get',
      data: {
        user_id: user_id,
        start_date: time,
        end_date: date.substring(0, 10),
      },
      headers: {
        Authorization: token,
      },
      datatype: 'application/json',
      success: function (res) {
        let sum = res.data;
        let avg = 0;
        let avg1 = 0;
        let avg2 = 0;
        let avg3 = 0;
        let days = 0;
        if (res.code == 10000) {
          arr = res.data.sleep_duration_month;
          for (let i = 0; i < arr.length; i++) {
            let a = arr[i].date.substring(5, 10).split('-')
            let temp_date_1 = parseInt(a[0]).toString()
            let temp_date_2 = parseInt(a[1]).toString()
            let b = temp_date_1 + '/' + temp_date_2
            value0.push(b)
          }
          for (let i = 0; i < arr.length; i++) {
            value1.push(arr[i].deep_sleep_duration);
            value2.push(arr[i].shallow_sleep_duration);
            value3.push(arr[i].clear_duration);
            if (
              arr[i].deep_sleep_duration +
              arr[i].shallow_sleep_duration +
              arr[i].clear_duration !=
              0
            ) {
              days = days + 1;
            }
            avg =
              avg +
              arr[i].deep_sleep_duration +
              arr[i].shallow_sleep_duration +
              arr[i].clear_duration;
            avg1 = avg1 + arr[i].deep_sleep_duration;
            avg2 = avg2 + arr[i].shallow_sleep_duration;
            avg3 = avg3 + arr[i].clear_duration;
          }
          let max;
          let max1 = Math.max(...value1);
          let max2 = Math.max(...value2);
          let max3 = Math.max(...value3);
          if (max1 > max2) {
            if (max1 > max3) {
              max = max1;
            } else {
              max = max3;
            }
          } else {
            if (max2 > max3) {
              max = max2;
            } else {
              max = max3;
            }
          }
          let lab_0 = value0[0]
          console.log('value0', value0)
          let lab_1 = value0[value0.length - 1]
          $('#monthlab').html(
            'Weekly averages, ' +
            lab_0 +
            '-' +
            lab_1
          );
          $('.subtext-week').html('Nightly average')
          //不同模块处理
          if (model == 'total') {
            var chart_month_total = echarts.init(document.getElementById('chart_month_total'));
            let s = Math.round(avg / days); //总平均
            let a1 = Math.round(avg1 / days); //深睡平均
            let a2 = Math.round(avg2 / days); //浅睡平均
            let a3 = Math.round(avg3 / days); //清醒平均
            let interval = Math.floor(value0.length / 4)
            console.log('interval', interval)
            drawweekchart(chart_month_total, value1, value2, value3, value0, interval, max);
            let sleep_duration = Math.round(
              (sum.deep_sleep_duration +
                sum.shallow_sleep_duration) /
              30
            );
            if (sleep_duration > 60) {
              let b = Math.floor(sleep_duration / 60);
              let c = sleep_duration % 60;
              $('.title-text-week3').html(b + ' hr ' + c + ' min');
            } else {
              let c = sleep_duration % 60;
              $('.title-text-week3').html(0 + ' hr ' + c + ' min');
            }
            if (a1 > 60) {
              let b = Math.floor(a1 / 60);
              let c = a1 % 60;
              $('.deep-data3').html(b + ' hr ' + c + ' min');
            } else {
              let c = a1.deep_sleep_duration % 60;
              $('.deep-data3').html(0 + ' hr ' + c + ' min');
            }
            if (a2 > 60) {
              let b = Math.floor(a2 / 60);
              let c = a2 % 60;
              $('.light-data3').html(b + ' hr ' + c + ' min');
            } else {
              let c = a2 % 60;
              $('.light-data3').html(0 + ' hr ' + c + ' min');
            }
            if (a3 > 60) {
              let b = Math.floor(a3 / 60);
              let c = a3 % 60;
              $('.awake-data3').html(b + ' hr ' + c + ' min');
            } else {
              let c = a3 % 60;
              $('.awake-data3').html(0 + ' hr ' + c + ' min');
            }
          } else if (model == 'deep') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = 'rgba(38, 84, 166, 1)';
            let color2 = 'rgba(38, 84, 166, 1)';
            let color3 = 'rgba(38, 84, 166, 0.2)';
            drawmonthchart(chart_month, value1, value0, color2, color3, color);
            let sleep_duration = Math.round(
              (sum.avg_deep_sleep_duration)
            );
            if (sleep_duration > 60) {
              let b = Math.floor(sleep_duration / 60);
              let c = sleep_duration % 60;
              $('#deep-data3').html(b + ' hr ' + c + ' min');
              $('.title-text-week3').html(b + ' hr ' + c + ' min');
            } else {
              let c = sleep_duration % 60;
              $('#deep-data3').html(0 + ' hr ' + c + ' min');
              $('.title-text-week3').html(b + ' hr ' + c + ' min');
            }
          } else if (model == 'light') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = 'rgba(38, 186, 171, 1)';
            let color2 = 'rgba(38, 186, 171, 1)';
            let color3 = 'rgba(38, 186, 171, 0.2)';
            drawmonthchart(chart_month, value2, value0, color2, color3, color);
            let sleep_duration = Math.round(
              (sum.avg_shallow_sleep_duration)
            );
            if (sleep_duration > 60) {
              let b = Math.floor(sleep_duration / 60);
              let c = sleep_duration % 60;
              $('#deep-data3').html(b + ' hr ' + c + ' min');
              $('.title-text-week3').html(b + ' hr ' + c + ' min');
            } else {
              let c = sleep_duration % 60;
              $('#deep-data3').html(0 + ' hr ' + c + ' min');
              $('.title-text-week3').html(b + ' hr ' + c + ' min');
            }
          } else if (model == 'heart') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = 'rgba(233, 73, 95, 1)';
            let color2 = 'rgba(233, 73, 95, 1)';
            let color3 = 'rgba(233, 73, 95, 0.2)';
            let heart = [];
            let sum = 0;
            let days = 0;
            let arr_1 = res.data.sleep_duration_month;
            for (let i = 0; i < arr_1.length; i++) {
              heart.push(arr_1[i].avg_heart_rate);
              if (arr_1[i].avg_heart_rate != 0) {
                sum = sum + arr_1[i].avg_heart_rate;
                days = days + 1;
              }
            }
            if (sum != 0) {
              $('.title-text-week3').html(Math.round(sum / days) + ' bpm');
            } else {
              $('.title-text-week3').html('0 bpm');
            }

            drawmonthchart(chart_month, heart, value0, color2, color3, color);
          } else if (model == 'breath') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = "rgba(73, 165, 222, 1)"
            let color2 = "rgba(73, 165, 222, 1)"
            let color3 = "rgba(73, 165, 222, 0.2)"
            let breath = [];
            let sum = 0;
            let days = 0;
            let arr_1 = res.data.avg_breath_rate;
            breath = res.data.breath_rate_stage
            $('.title-text-week3').html(arr_1 + ' breaths/min');
            drawmonthchart(chart_month, breath, value0, color2, color3, color);
          } else if (model == 'move') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = 'rgba(138, 134, 226, 1)';
            let color2 = 'rgba(138, 134, 226, 1)';
            let color3 = 'rgba(138, 134, 226, 0.2)';
            let move = [];
            let sum = 0;
            let days = 0;
            let arr_1 = res.data.sleep_duration_month;
            for (let i = 0; i < arr_1.length; i++) {
              move.push(arr_1[i].twitch_times);
              if (arr_1[i].twitch_times != 0) {
                sum = sum + arr_1[i].twitch_times;
                days = days + 1;
              }
            }
            if (sum != 0) {
              $('.title-text-week3').html(
                Math.round(sum / days) + (Math.round(sum / days) == 1 ? ' movement' : ' movements')
              );
            } else {
              $('.title-text-week3').html('0 movements');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_month, move, value0, color2, color3, color);
          } else if (model == 'hrv') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let data = res.data.rmssd_stage;
            let color = 'rgba(243, 193, 83, 1)';
            let color2 = 'rgba(243, 193, 83, 1)';
            let color3 = 'rgba(243, 193, 83, 0.2)';
            let avg3 = 0;
            let rmssd = [];
            let days = 0;
            for (let i = 0; i < value0.length; i++) {
              if (data[i] != 0) {
                days = days + 1;
              }
              avg3 = avg3 + data[i];
              rmssd.push(data[i]);
            }
            $('.hrv_month_num3 ').html(Math.round(avg3 / days));
            if (avg3 != 0) {
              $('.title-text-week3').html(
                Math.round(avg3 / days) + ' ms'
              );
            } else {
              $('.title-text-week3').html('0 ms');
            }
            drawmonthchart(chart_month, rmssd, value0, color2, color3, color);
            $('.hrv_week_daterow').html(
              'Nightly averages,' +
              lab_0[0] +
              '-' +
              lab_1[1]
            );
          } else if (model == 'snore') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = 'rgba(109, 173, 143, 1)';
            let color2 = 'rgba(109, 173, 143, 1)';
            let color3 = 'rgba(109, 173, 143, 0.2)';
            let snore = [];
            let avg = 0;
            let avg2 = 0;
            let days = 0;
            let sum0 = res.data.snore_times_stage;
            let sum = res.data.sleep_duration_month;
            for (let i = 0; i < sum.length; i++) {
              days = days + 1;
              snore.push(sum[i].anti_snore_times);
              avg = avg + sum[i].anti_snore_times;
            }
            for (let i = 0; i < sum0.length; i++) {
              avg2 = avg2 + sum0[i];
            }
            if (avg2 != 0) {
              $('.title-text-week3').html(
                Math.round(avg2 / sum0.length) + (Math.round(avg2 / sum0.length) == 1 ? ' time' : ' times')
              );
            } else {
              $('.title-text-week3').html('0 times');
            }
            if (avg != 0) {
              $('#top-text3').html(
                Math.round(avg / days) + (Math.round(avg / days) == 1 ? ' auto adjustment' : ' auto adjustments')
              );
            } else {
              $('#top-text3').html('0 auto adjustments');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_month, snore, value0, color2, color3, color);
          } else if (model == 'outbed') {
            var chart_month = echarts.init(document.getElementById('chart_month'));
            let color = 'rgba(240, 92, 73, 1)'
            let color2 = 'rgba(240, 92, 73, 1)'
            let color3 = 'rgba(240, 92, 73, 0.2)'
            let temp = res.data.left_bed_times
            let temp2 = res.data.left_bed_time
            if (temp2 != 1) {
              $('.title-text-week3').html(temp2 + ' times');
            } else {
              $('.title-text-week3').html(temp2 + ' time');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_month, temp, value0, color2, color3, color)
          }
        }
      },
    });
  }
  //年报
  function getyearreport () {
    let arr = [];
    let value0 = [];
    let value1 = [];
    let value2 = [];
    let value3 = [];
    let time = dateformat(
      new Date(date.replace(/-/g, '/')).getTime() - 24 * 60 * 60 * 365 * 1000,
      0,
      2
    );
    let url = '';
    if (host == 1) {
      url =
        'https://wellness.ergomotion.com/ergoZone/api/v1/sleep/getSleepYear';
    } else {
      url =
        'https://resthomeus.smartbed.top/ergoZone/api/v1/sleep/getSleepYear';
    }
    let temp2 = new Date(time.replace(/-/g, '/') + " 00:00:00")
    let yn2 = temp2.getFullYear()
    let mn2 = temp2.getMonth()
    let dn2 = temp2.getDate()

    $.ajax({
      url: url,
      type: 'get',
      data: {
        user_id: user_id,
        start_date: time,
        end_date: date.substring(0, 10),
      },
      headers: {
        Authorization: token,
      },
      datatype: 'application/json',
      success: function (res) {
        let sum = res.data;
        let avg = 0;
        let avg1 = 0;
        let avg2 = 0;
        let avg3 = 0;
        let days = 0;
        if (res.code == 10000) {
          let tempdate = new Date(res.data.date[0].replace(/-/g, '/') + ' 00:00:00')
          let mn3 = tempdate.getMonth()
          let dn3 = tempdate.getDate()
          if (mn2 == mn3 && dn2 == dn3) {
            if (yn2 != yn) {
              $('#time-toptext').html(MonthArr[mn2] + ' ' + dn2 + ', ' + yn2 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
            } else {
              $('#time-toptext').html(MonthArr[mn2] + ' ' + dn2 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
            }
          } else {
            if (yn2 != yn) {
              $('#time-toptext').html(MonthArr[mn3] + ' ' + dn3 + ', ' + yn2 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
            } else {
              $('#time-toptext').html(MonthArr[mn3] + ' ' + dn3 + ' - ' + MonthArr[mn] + ' ' + dn + ', ' + yn)
            }
          }
          arr = res.data.sleep_duration_year;
          for (let i = 0; i < arr.length; i++) {
            let a = arr[i].date.substring(5, 10).split('-')
            let temp_date_1 = parseInt(a[0]).toString()
            let temp_date_2 = parseInt(a[1]).toString()
            let b = temp_date_1 + '/' + temp_date_2
            value0.push(b)
          }
          for (let i = 0; i < arr.length; i++) {
            value1.push(arr[i].deep_sleep_duration);
            value2.push(arr[i].shallow_sleep_duration);
            value3.push(arr[i].clear_duration);
            if (
              arr[i].deep_sleep_duration +
              arr[i].shallow_sleep_duration +
              arr[i].clear_duration !=
              0
            ) {
              days = days + 1;
            }
            avg =
              avg +
              arr[i].deep_sleep_duration +
              arr[i].shallow_sleep_duration +
              arr[i].clear_duration;
            avg1 = avg1 + arr[i].deep_sleep_duration;
            avg2 = avg2 + arr[i].shallow_sleep_duration;
            avg3 = avg3 + arr[i].clear_duration;
          }
          let max;
          let max1 = Math.max(...value1);
          let max2 = Math.max(...value2);
          let max3 = Math.max(...value3);
          if (max1 > max2) {
            if (max1 > max3) {
              max = max1;
            } else {
              max = max3;
            }
          } else {
            if (max2 > max3) {
              max = max2;
            } else {
              max = max3;
            }
          }
          let lab_0 = value0[0]
          let lab_1 = value0[value0.length - 1]
          if (yn2 != yn) {
            $('#yearlab').html(
              'Monthly Averages, ' +
              lab_0 + '/' + yn2.toString().substring(2, 4) +
              '-' +
              lab_1 + '/' + yn.toString().substring(2, 4)
            );
          } else {
            $('#yearlab').html(
              'Monthly Averages, ' +
              lab_0 +
              '-' +
              lab_1
            );
          }
          $('.subtext-week').html('Nightly average')
          //不同模块处理
          if (model == 'total') {
            var chart_year_total = echarts.init(document.getElementById('chart_year_total'));
            let s = Math.round(avg / days); //总平均
            let a1 = Math.round(avg1 / days); //深睡平均
            let a2 = Math.round(avg2 / days); //浅睡平均
            let a3 = Math.round(avg3 / days); //清醒平均
            drawweekchart(chart_year_total, value1, value2, value3, value0, max);
            let sleep_duration = Math.round(
              (sum.avg_deep_sleep_duration +
                sum.avg_shallow_sleep_duration)
            );
            if (sleep_duration > 60) {
              let b = Math.floor(sleep_duration / 60);
              let c = sleep_duration % 60;
              $('.title-text-week3').html(b + ' hr ' + c + ' min');
            } else {
              let c = sleep_duration % 60;
              $('.title-text-week3').html(0 + ' hr ' + c + ' min');
            }
            if (a1 > 60) {
              let b = Math.floor(a1 / 60);
              let c = a1 % 60;
              $('.deep-data3').html(b + ' hr ' + c + ' min');
            } else {
              let c = a1.deep_sleep_duration % 60;
              $('.deep-data3').html(0 + ' hr ' + c + ' min');
            }
            if (a2 > 60) {
              let b = Math.floor(a2 / 60);
              let c = a2 % 60;
              $('.light-data3').html(b + ' hr ' + c + ' min');
            } else {
              let c = a2 % 60;
              $('.light-data3').html(0 + ' hr ' + c + ' min');
            }
            if (a3 > 60) {
              let b = Math.floor(a3 / 60);
              let c = a3 % 60;
              $('.awake-data3').html(b + ' hr ' + c + ' min');
            } else {
              let c = a3 % 60;
              $('.awake-data3').html(0 + ' hr ' + c + ' min');
            }
          } else if (model == 'deep') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = 'rgba(111, 148, 216, 1)';
            let color2 = 'rgba(111, 148, 216, 1)';
            let color3 = 'rgba(111, 148, 216, 0.2)';
            drawmonthchart(chart_year, value1, value0, color2, color3, color);
            let sleep_duration = Math.round(
              (sum.avg_deep_sleep_duration)
            );
            if (sleep_duration > 60) {
              let b = Math.floor(sleep_duration / 60);
              let c = sleep_duration % 60;
              $('#deep-data3').html(b + ' hr ' + c + ' min');
              $('#title-text-day3').html(b + ' hr ' + c + ' min');
            } else {
              let c = sleep_duration % 60;
              $('#deep-data3').html(0 + ' hr ' + c + ' min');
              $('#title-text-day3').html(b + ' hr ' + c + ' min');
            }
          } else if (model == 'light') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = 'rgba(38, 186, 171, 1)';
            let color2 = 'rgba(38, 186, 171, 1)';
            let color3 = 'rgba(38, 186, 171, 0.2)';
            drawmonthchart(chart_year, value2, value0, color2, color3, color);
            let sleep_duration = Math.round(
              sum.avg_shallow_sleep_duration)
              ;
            if (sleep_duration > 60) {
              let b = Math.floor(sleep_duration / 60);
              let c = sleep_duration % 60;
              $('#deep-data3').html(b + ' hr ' + c + ' min');
              $('#title-text-day3').html(b + ' hr ' + c + ' min');
            } else {
              let c = sleep_duration % 60;
              $('#deep-data3').html(0 + ' hr ' + c + ' min');
              $('#title-text-day3').html(b + ' hr ' + c + ' min');
            }
          } else if (model == 'heart') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = 'rgba(233, 73, 95, 1)';
            let color2 = 'rgba(233, 73, 95, 1)';
            let color3 = 'rgba(233, 73, 95, 0.2)';
            let date = [];
            let sum = 0;
            let days = 0;
            // let arr_1 = res.data.date;
            // for (let i = 0; i < arr_1.length; i++) {
            //   date.push(arr_1[i].substring(5, 10).replace(/-/g, '/'));
            // }
            if (res.data.avg_heart_rate != 0) {
              $('.title-text-week3').html(res.data.avg_heart_rate + ' bpm');
            } else {
              $('.title-text-week3').html('0 bpm');
            }

            drawmonthchart(chart_year, res.data.heart_rate_stage, value0, color2, color3, color);
          } else if (model == 'breath') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = "rgba(73, 165, 222, 1)"
            let color2 = "rgba(73, 165, 222, 1)"
            let color3 = "rgba(73, 165, 222, 0.2)"
            let breath = [];
            let sum = 0;
            let days = 0;
            let arr_1 = res.data.avg_breath_rate;
            breath = res.data.breath_rate_stage
            $('.title-text-week3').html(arr_1 + ' breaths/min');
            drawmonthchart(chart_year, breath, value0, color2, color3, color);
          } else if (model == 'move') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = 'rgba(138, 134, 226, 1)';
            let color2 = 'rgba(138, 134, 226, 1)';
            let color3 = 'rgba(138, 134, 226, 0.2)';
            let move = [];
            let sum = 0;
            let days = 0;
            let arr_1 = res.data.sleep_duration_year;
            for (let i = 0; i < arr_1.length; i++) {
              move.push(arr_1[i].twitch_times);
              if (arr_1[i].twitch_times != 0) {
                sum = sum + arr_1[i].twitch_times;
                days = days + 1;
              }
            }
            if (sum != 0) {
              $('.title-text-week3').html(
                Math.round(sum / days) + ' movements'
              );
            } else {
              $('.title-text-week3').html('0 movements');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_year, move, value0, color2, color3, color);
          } else if (model == 'hrv') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let data = res.data.rmssd_stage;
            let color = 'rgba(243, 193, 83, 1)';
            let color2 = 'rgba(243, 193, 83, 1)';
            let color3 = 'rgba(243, 193, 83, 0.2)';
            let avg3 = 0;
            let rmssd = [];
            let days = 0;
            for (let i = 0; i < value0.length; i++) {
              if (data[i] != 0) {
                days = days + 1;
              }
              avg3 = avg3 + data[i];

              rmssd.push(data[i]);

            }

            // $('.hrv_month_num3 ').html(Math.round(avg3 / days));
            if (avg3 != 0) {
              $('.title-text-week3').html(
                Math.round(avg3 / days) + ' ms'
              );
            } else {
              $('.title-text-week3').html('0 ms');
            }
            drawmonthchart(chart_year, rmssd, value0, color2, color3, color);
            $('.hrv_week_daterow').html(
              'Nightly averages,' +
              lab_0[0] +
              '-' +
              lab_1[1]
            );
          } else if (model == 'snore') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = 'rgba(109, 173, 143, 1)';
            let color2 = 'rgba(109, 173, 143, 1)';
            let color3 = 'rgba(109, 173, 143, 0.2)';
            let snore = [];
            let avg = 0;
            let avg2 = 0;
            let days = 0;
            let sum0 = res.data.snore_times_stage;
            let sum = res.data.sleep_duration_year;
            for (let i = 0; i < sum.length; i++) {
              days = days + 1;
              snore.push(sum[i].anti_snore_times);
              avg = avg + sum[i].anti_snore_times;
            }
            for (let i = 0; i < sum0.length; i++) {
              avg2 = avg2 + sum0[i];
            }
            if (avg2 != 0) {
              $('.title-text-week3').html(
                Math.round(avg2 / sum0.length) + ' times'
              );
            } else {
              $('.title-text-week3').html('0 times');
            }
            if (avg != 0) {
              $('#top-text4').html(
                Math.round(avg / days) + ' auto adjustments'
              );
            } else {
              $('#top-text4').html('0 auto adjustments');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_year, snore, value0, color2, color3, color);
          } else if (model == 'outbed') {
            var chart_year = echarts.init(document.getElementById('chart_year'));
            let color = 'rgba(240, 92, 73, 1)'
            let color2 = 'rgba(240, 92, 73, 1)'
            let color3 = 'rgba(240, 92, 73, 0.2)'
            let temp = res.data.left_bed_times
            let temp2 = res.data.left_bed_time
            if (temp2 != 1) {
              $('.title-text-week3').html(temp2 + ' times');
            } else {
              $('.title-text-week3').html(temp2 + ' time');
            }
            $('.subtext-week').html('Nightly average')
            drawmonthchart(chart_year, temp, value0, color2, color3, color)
          }
        }
      },
    });
  }
</script>

</html>